package cn.itcast.bos.web.action.base;


import com.alibaba.fastjson.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierSerivce;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Actions
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {
	@Autowired
	private CourierSerivce courierService;
	private Courier courier = new Courier();
	public Integer page;
	public  Integer rows;
	public String ids ;

	@Override
	public Courier getModel() {
		// TODO Auto-generated method stub
		return courier;
	}
	@Action(value="curierAction_save",
			results={@Result(name="success",location="/pages/base/courier.html",type = "redirect")})
	public String saveCourier(){
		courierService.saveCourier(courier);
		return SUCCESS;
	}
	/*@Action(value = "courier_pageQuery")
	public String courier_pageQuery() throws IOException {
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Courier> pageData = courierService.pageQuery(pageable);
		//创建一个map
		Map<String,Object> couriers = new HashMap();
		couriers.put("total",pageData.getTotalElements());
		couriers.put("rows",pageData.getContent());
		String json = JSONArray.toJSONString(couriers);
		ServletActionContext.getResponse().getWriter().write(json);
		return  null;
	}*/
	//分页条件查询
	@Action(value = "courier_pageQuery")
	public String courier_pageQuery() throws IOException {
		Pageable pageable = new PageRequest(page-1,rows);
		Page<Courier> pageData = courierService.pageQuery(pageable,courier);
		//创建一个map
		Map<String,Object> couriers = new HashMap();
		couriers.put("total",pageData.getTotalElements());
		couriers.put("rows",pageData.getContent());
		String json = JSONArray.toJSONString(couriers);
		ServletActionContext.getResponse().getWriter().write(json);
		return  null;
	}//删除功能  //将deltag值修改为1 
	@Action(value ="courier_delete",
			results={@Result(name="success",location="/pages/base/courier.html",type = "redirect")})
	public String courier_delete(){
			courierService.delete(ids);
		return SUCCESS;
	}
	@Action(value = "courier_findnoassociation")
	public String findnoassociation() throws IOException {
		//调用业务层,查询未关联定区的快递员
		List<Courier> couriers = courierService.findNoAssociation();
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		String  json = JSONArray.toJSONString(couriers);

		ServletActionContext.getResponse().getWriter().write(json);
		return  NONE;
	}
	
}
